package com.wtb.javatool.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtb.javatool.constant.evaluation.AssetsType;
import com.wtb.javatool.dao.AssetsMapper;
import com.wtb.javatool.dao.CatalogMapper;
import com.wtb.javatool.entity.Assets;
import com.wtb.javatool.service.AssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class AssetsServiceImpl extends ServiceImpl<AssetsMapper, Assets> implements AssetsService {

    @Autowired
    private AssetsMapper assetsMapper;
    @Autowired
    private CatalogMapper catalogMapper;

    @Override
    public List<Assets> getAssetsByBandId(Long bandId) {
        return assetsMapper.getAssetsByBandId(bandId);
    }

    @Override
    public Assets getById(Long id) {
        return assetsMapper.getById(id);
    }

    @Override
    public List<Assets> getByDocumentId(Long documentId) {
        return assetsMapper.getByDocumentId(documentId);
    }

    @Override
    public Long insert(Assets assets) {
        Assets existAssets = assetsMapper.getByDocumentIdAndCatalogId(assets.getDocumentId(), assets.getCatalogId());
        //是否需要查询该目录是否存在再添加资源
        if (existAssets != null && !existAssets.getType().equals(AssetsType.LINK)) {
            // 如果已存在，则返回重复提示
            return Long.valueOf(-2);
        } else {
            // 如果不存在，则插入新记录
            int insertResult = assetsMapper.insert(assets);
            if (insertResult > 0) {
                // 获取新生成的ID
                Long newId = assets.getId();
                return newId;
            } else {
                return Long.valueOf(-1);
            }
        }
    }

    @Override
    public int update(Assets assets) {
        return assetsMapper.update(assets);
    }

    @Override
    public int delete(Long id) {
        return assetsMapper.delete(id);
    }

    @Override
    public int deleteMany(List<Long> ids) {
        return assetsMapper.deleteMany(ids);
    }

    @Override
    public int deleteByDocumentId(Long documentId) {
        return assetsMapper.deleteByDocumentId(documentId);
    }

    @Override
    public int deleteByCatalogId(Long catalogId) {
        return assetsMapper.deleteByCatalogId(catalogId);
    }

    @Override
    @Transactional
    public int deleteManyByCatalogId(List<Long> catalogIds) {
        return assetsMapper.deleteManyByCatalogId(catalogIds);
    }

}